﻿using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;


static public class EditorTool
{

    static public void SetLabelWidth(float width)
    {
        EditorGUIUtility.labelWidth = width;
    }


    static public void SetDirty(UnityEngine.Object obj)
    {
#if UNITY_EDITOR
        if (obj)
        {
            UnityEditor.EditorUtility.SetDirty(obj);
        }
#endif
    }


    static public void RegisterUndo(string name, params Object[] objects)
    {
        if (objects != null && objects.Length > 0)
        {
            UnityEditor.Undo.RecordObjects(objects, name);

            foreach (Object obj in objects)
            {
                if (obj == null) continue;
                EditorUtility.SetDirty(obj);
            }
        }
    }

    static public bool DrawHeader(string text, string key)
    {
        bool state = EditorPrefs.GetBool(key, true);

        if (!state) GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
        GUILayout.BeginHorizontal();
        GUI.changed = false;


        if (state) text = "\u25BC" + (char)0x200a + text;
        else text = "\u25BA" + (char)0x200a + text;

        GUILayout.BeginHorizontal();
        GUI.contentColor = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.7f) : new Color(0f, 0f, 0f, 0.7f);
        if (!GUILayout.Toggle(true, text, "PreToolbar2", GUILayout.MinWidth(20f))) state = !state;
        GUI.contentColor = Color.white;
        GUILayout.EndHorizontal();



        if (GUI.changed) EditorPrefs.SetBool(key, state);

        GUILayout.EndHorizontal();
        GUI.backgroundColor = Color.white;
        if (!state) GUILayout.Space(3f);
        return state;
    }

    static public void BeginContents()
    {        
        //    mEndHorizontal = false;
        EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(10f));
        GUILayout.Space(10f);

        GUILayout.BeginVertical();
        GUILayout.Space(2f);
    }

    static public void EndContents()
    {
        GUILayout.Space(3f);
        GUILayout.EndVertical();
        EditorGUILayout.EndHorizontal();

//         if (mEndHorizontal)
//         {
//             GUILayout.Space(3f);
//             GUILayout.EndHorizontal();
//         }

        GUILayout.Space(3f);
    }

    static public void DrawEvents(string text, Object undoObject, List<EventDelegate> list)
    {
        if (!DrawHeader(text, text))
            return;       
        EventDelegateEditor.Field(undoObject, list);
    }


    static public string GetFuncName(object obj, string method)
    {
        if (obj == null) return "<null>";
        string type = obj.GetType().ToString();
        int period = type.LastIndexOf('/');
        if (period > 0) type = type.Substring(period + 1);
        return string.IsNullOrEmpty(method) ? type : type + "/" + method;
    }

}

